package com.zstu.mall.web.admin;

import com.zstu.mall.entity.Order;
import com.zstu.mall.entity.UserOrder;
import com.zstu.mall.entity.pojo.ResultBean;
import com.zstu.mall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/admin/order")
public class AdminOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 打开订单列表页面
     * @return
     */
    @RequestMapping("/toList.html")
    public String toList() {
        return "mall/admin/order/orderManage";
    }

    /**
     * 获取所有订单的总数
     * @return
     */
    @ResponseBody
    @RequestMapping("/getTotal.do")
    public ResultBean<Integer> getTotal() {
        Pageable pageable = PageRequest.of(1, 15);
        int total = (int) orderService.findByPage(pageable).getTotalElements();
        System.out.println(total);
        return new ResultBean<>(total);
    }

    /**
     * 获取所有订单
     * @return
     */
    @ResponseBody
    @RequestMapping("/list.do")
    public ResultBean<List<Order>> listData() {
        List<Order> list = orderService.findAll();
        //为订单列表添加具体订单信息
        for(Order order : list){
            List<UserOrder> orderItems = orderService.findItems(order.getId());
            order.setUserOrders(orderItems);
        }
        return new ResultBean<>(list);
    }

    /**
     * 获取订单项
     * @param orderId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getDetail.do")
    public ResultBean<List<UserOrder>> getDetail(String orderId) {
        List<UserOrder> list = orderService.findItems(orderId);
        return new ResultBean<>(list);
    }

    /**
     * 发货
     * @param id
     * @return
     */
    @RequestMapping("/send.do")
    public String send(String id) {
        orderService.updateStatus(id,"3");
        return "mall/admin/order/orderManage";
    }

}
